package by.demchenko.sample.webapp.page.home.students;

import by.demchenko.sample.datamodel.Person;
import by.demchenko.sample.datamodel.Person_;
import by.demchenko.sample.datamodel.Result;
import by.demchenko.sample.datamodel.Result_;
import by.demchenko.sample.datamodel.enums.UserType;
import by.demchenko.sample.services.ResultService;
import by.demchenko.sample.webapp.app.BasicAuthenticationSession;
import by.demchenko.sample.webapp.page.home.BaseLayout;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;

import javax.inject.Inject;
import javax.persistence.Basic;
import javax.persistence.metamodel.SingularAttribute;
import java.util.Iterator;
import java.util.List;

public class GradesPage extends BaseLayout {

    @Inject
    ResultService resultService;

    public GradesPage() {
        List<Result> grades = resultService.getGrades(BasicAuthenticationSession.get().getUser());

        ResultDataProvider resultDataProvider = new ResultDataProvider();

        DataView<Result> dataView = new DataView<Result>("resultsList", resultDataProvider) {
            @Override
            protected void populateItem(Item<Result> item) {
                final Result result = item.getModelObject();
                item.add(new Label("section", result.getSchedule().getCourse().getSection().getName()));
                item.add(new Label("course", result.getSchedule().getCourse().getName()));
                item.add(new Label("teacher", result.getSchedule().getCourse().getTeacher().getFullName()));
                item.add(new Label("grade", result.getGrade()));
            };
        };

        add(dataView);
    }

    private class ResultDataProvider extends SortableDataProvider<Result, SingularAttribute<Result, ?>> {

        public ResultDataProvider() {
            super();
            setSort(Result_.schedule, SortOrder.ASCENDING);
        }

        @Override
        public Iterator<? extends Result> iterator(long l, long l1) {
            SingularAttribute<Result, ?> sortParam = getSort().getProperty();
            SortOrder propertySortOrder = getSortState().getPropertySortOrder(sortParam);
            //boolean ascending = SortOrder.ASCENDING.equals(propertySortOrder);
            return resultService.getGrades(BasicAuthenticationSession.get().getUser()).iterator();

        }

        @Override
        public long size() {
            return resultService.getCount();
        }

        @Override
        public IModel<Result> model(Result result) {
            return new CompoundPropertyModel<>(result);
        }
    }
}
